<?php

namespace App\Http\Controllers\Admin\UserManager;

use App\Services\Tools\Export;
use App\Exceptions\BasicException;
use App\Http\Controllers\BaseController;
use Illuminate\Http\Request;

//引用http请求类
use App\Response\Response;
use Illuminate\Support\Facades\Validator;

class UserSealController extends BaseController
{

    /**
     * @api {delete} /user-manager/user-ban-list 用户封禁记录删除
     * @apiVersion 0.1.0
     * @apiName 会员管理-用户封禁记录删除
     * @apiGroup 会员管理
     * @apiParam {string} ids    字符串分割的id 必传
     * @apiSuccessExample 成功响应
     *      {"code":200,"data":true}
     * @apiError 10001 参数校验失败
     * @apiErrorExample 错误响应
     *      {
     *          "code": 10001,
     *          "message": "errorMsg"
     *      }
     */
    public function del(Request $request)
    {
        //参数校验
        try {
            $this->validate($request, [
                'ids' => 'required|string',
            ]);
        } catch (\Exception $e) {
            throw new BasicException(10001, json_encode($e->errors()));
        }

        $idsStr = $request->input("ids");
        $idsArr = explode(",", $idsStr);

        $rows = app('repo_user')->delUserBan($idsArr);
        if (!$rows) {
            throw new BasicException(10001, "删除失败！");
        }

        return Response::sendData(true);
    }

    /**
     * @api {get} /user-manager/user-ban-list 用户封禁记录列表
     * @apiVersion 0.1.0
     * @apiName 会员管理-用户封禁记录列表
     * @apiGroup 会员管理
     * @apiParam {number} user_code 用户编号 可选
     * @apiParam {number} admin_id 管理员id 可选
     * @apiParam {string} mobile 用户电话 可选
     * @apiParam {string} nickname 用户昵称 可选
     * @apiParam {string} remark 封禁注释 可选
     * @apiParam {number} start_time 开始时间 可选
     * @apiParam {number} end_time 结束时间 可选
     * @apiParam {number} page 当前页数 可选
     * @apiParam {number} perPage 分页容量 可选
     * @apiSuccessExample 成功响应
     * {
     * "code": 200,
     * "data": {
     * "list": [
     * {
     * "id(序号)": 329,
     * "add_time(封禁时间)": "2021-05-13 00:00:00",
     * "seal_time(封禁截止时间)": "2032-11-13 00:00:00",
     * "remark(封禁理由)": "诈骗",
     * "nickname(昵称)": "开开心的里",
     * "user_code(用户编号)": 846301,
     * "mobile(手机号)": "13480822667"
     * }
     * ],
     * "meta": {
     * "page": 1,
     * "limit": 10,
     * "next": null,
     * "total": 1
     * }
     * }
     * }
     */
    public function list(Request $request)
    {
        //参数校验
        //        try {
        $validator = Validator::make($request->all(), [
            'user_code' => 'filled|numeric',
            'admin_id'  => 'filled|numeric',
            'mobile'    => 'filled|numeric',
            'nickname'  => 'filled|string|max:255',
            'remark'    => 'filled|string|max:255',

            'start_time' => 'filled|numeric',
            'end_time'   => 'filled|numeric',
            'perPage'    => 'filled|numeric',
            'page'       => 'filled|numeric',
        ], [
            'user_code.filled'  => '用户ID不能为空',
            'user_code.numeric' => '用户ID必须为数字',
        ]);
        if ($validator->fails()) {
            throw new BasicException(10001, $validator->errors()->first());
        }
        //        } catch (\Exception $e) {
        //            throw new BasicException(10001, json_encode($e->errors()));
        //        }

        $list = app('repo_user')->getUserBanQueryList($request->input());

        return Response::sendList($list);
    }

    /**
     * @api {get} /user-manager/user-ban-list/excel 用户封禁记录导出
     * @apiVersion 0.1.0
     * @apiName 会员管理-用户封禁记录导出
     * @apiGroup 会员管理
     * @apiParam {number} user_code 用户编号 可选
     * @apiParam {number} admin_id 管理员id 可选
     * @apiParam {string} mobile 用户电话 可选
     * @apiParam {string} nickname 用户昵称 可选
     * @apiParam {string} remark 封禁注释 可选
     * @apiParam {number} start_time 开始时间 可选
     * @apiParam {number} end_time 结束时间 可选
     * @apiParam {number} page 当前页数 可选
     * @apiParam {number} perPage 分页容量 可选
     * @apiSuccessExample 成功响应
     * {
     * "code": 200,
     * "data": {
     * "uri": "asdasdasd.com"
     * }
     */
    public function toExcel(Request $request)
    {
        //参数校验
        $validator = Validator::make($request->all(), [
            'user_code' => 'filled|numeric',
            'admin_id'  => 'filled|numeric',
            'mobile'    => 'filled|numeric',
            'nickname'  => 'filled|string|max:255',
            'remark'    => 'filled|string|max:255',

            'start_time' => 'filled|numeric',
            'end_time'   => 'filled|numeric',
        ], [
            'user_code.filled'  => '用户ID不能为空',
            'user_code.numeric' => '用户ID必须为数字',
        ]);
        if ($validator->fails()) {
            throw new BasicException(10001, $validator->errors()->first());
        }

        $res = app('repo_user')->getUserBanQueryAll($request->input());


        $fields = [
            ['field' => 'id', 'header' => '序号', 'width' => '18'],
            ['field' => 'user_code', 'header' => '用户编号', 'width' => '18'],
            ['field' => 'nickname', 'header' => '昵称', 'width' => '18'],
            ['field' => 'mobile', 'header' => '用户手机号', 'width' => '18'],
            ['field' => 'reason', 'header' => '封禁原因', 'width' => '18'],
            ['field' => 'seal_time_string', 'header' => '封禁时长', 'width' => '18'],
            ['field' => 'source', 'header' => '封禁来源', 'width' => '18'],
            ['field' => 'remark', 'header' => '详情说明', 'width' => '80'],
            ['field' => 'add_time', 'header' => '封禁开始时间', 'width' => '18'],
            ['field' => 'seal_time', 'header' => '封禁结束时间', 'width' => '18'],
            ['field' => 'is_ban_auth', 'header' => '是否封禁关联账号', 'width' => '18'],
            ['field' => 'admin_name', 'header' => '操作管理员', 'width' => '18'],
        ];

        $downloadUrl = Export::exportUsePhpOffice('user_seal', $fields, $res);
        app('model_admin_export_log')->add(25, $request->input(), $downloadUrl);
        return Response::sendData(['uri' => $downloadUrl]);
    }



}
